package com.gs.wzmanager.controller;

import com.gs.wzmanager.entity.Resource;
import com.gs.wzmanager.service.ResourceService;
import com.gs.wzmanager.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/res")
public class ResourceController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ResourceService resourceService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity add(@RequestBody  Resource resource) {
        logger.info("resource->{}",resource);
        resourceService.addResource(resource);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseEntity update(@RequestBody  Resource resource) {
        resourceService.updateResource(resource);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }

    @RequestMapping("/listView")
    public String listView(){
        return "res/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public List list() {
        List<Resource> resources = resourceService.queryAllResource();
        return resources;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ResponseEntity del(@PathVariable int id) {
        resourceService.deleteResourceById(id);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }

}
